package com.app.piemoment;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.app.piemoment.api.ApiResult;
import com.app.piemoment.api.ApiReturnResultListener;
import com.app.piemoment.api.TestApi;

public class TestResultActivity extends BaseActivity {
	
	private TextView result_text;
	private TextView module_text;

	public TestResultActivity() {
		mainViewId = R.layout.result_activity;
	}

	@Override
	protected void initMainView() {
		initTitle(R.drawable.btn_back_bg, "测试结果", 0, 0);
		module_text = (TextView)findViewById(R.id.textView1);
		result_text = (TextView)findViewById(R.id.textView2);

	}

	@Override
	protected void registerMainViewEvent() {

	}

	@Override
	protected void processMainView() {
		String module = getIntent().getStringExtra("module");
		getModuleResult(module);
	}
	
	private void getModuleResult(String module) {
		TestApi api = new TestApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {
			
			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(LOAD_PROGRESS);
				//{"status":"success","data":{"module":"\u4ea4\u5f80\u6a21\u5f0f","report":"report example"}}

				if(ApiResult.RESULT_OK==apiResult.getResultCode()) {
					JSONObject jsonEntity = apiResult.getJsonEntity();
					try {
						if (jsonEntity != null) {
							if(jsonEntity.has("status")) {
								String result = jsonEntity.getString("status");
								if(ApiResult.Success.equals(result)) {
									JSONObject obj = jsonEntity.getJSONObject("data");
									if(obj!=null&&obj.has("report")) {
										result_text.setText(obj.getString("report"));
										module_text.setText(obj.getString("module"));
									}
								}
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
			}
			
			@Override
			public <T> void onReturnLoading(int requestCode) {
				Log.e("DIALOG", "showdialog");
				showDialog(LOAD_PROGRESS);
			}
			
			@Override
			public <T> void onReturnFailResult(int requestCode, ApiResult<T> apiResult) {
				dismissDialog(LOAD_PROGRESS);
			}
		});
		api.getModouleResults(1, this, module);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			super.onBackPressed();
			break;

		default:
			break;
		}
	}


}
